package com.skyvin.nreceipts.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyvin.nreceipts.admin.mongorepository.RetailerRepository;
import com.skyvin.nreceipts.admin.service.RetailerService;
import com.skyvin.nreceipts.common.controller.BaseController;
import com.skyvin.nreceipts.common.document.Retailer;
import com.skyvin.nreceipts.common.value.JQGridResponse;
import com.skyvin.nreceipts.common.value.MessageValue;

@Controller
@RequestMapping(value = "retailer")
public class RetailerController extends BaseController {
	private static Logger logger = Logger.getLogger(RetailerController.class);

	@Autowired
	private RetailerRepository retailerRepository;
	
	@Autowired
	private RetailerService retailerService;
	
	@Autowired
	private MongoTemplate mongoTemplate;

	@RequestMapping(value = "/loadRetailerPage")
	public String load() {
		return "admin/retailerEdit";
	}
	
	@RequestMapping(value="/viewRetailers", method=RequestMethod.GET)
	@ResponseBody
	public JQGridResponse viewMyRetailers(@RequestParam("_search") Boolean search,
    		@RequestParam(value="filters", required=false) String filters,
    		@RequestParam(value="page", required=false) Integer page,
    		@RequestParam(value="rows", required=false) Integer rows,
    		@RequestParam(value="sidx", required=false) String sidx,
    		@RequestParam(value="sord", required=false) String sord) {
		
		List<Retailer> retailerList = null;
		//Page<Retailer> retailerPageList = null;
		
		JQGridResponse jqGridResponse = new JQGridResponse();
		Pageable pageRequest = new PageRequest(page-1, rows);
		long count = 0;
		int countOfRetailerListReturned = 0;
		
		//retailerPageList = retailerRepository.findAll(pageRequest);
		Query query = new Query();
		query.addCriteria(Criteria.where("userPID").is(getCurrentUserPID()));
		query.skip((page-1) * rows);
		query.limit(rows);
		
		Query countQuery = new Query();
		countQuery.addCriteria(Criteria.where("userPID").is(getCurrentUserPID()));
		
		retailerList = mongoTemplate.find(query, Retailer.class, retailerService.getMyCollection(Retailer.class));
		if(retailerList!= null) {
			countOfRetailerListReturned = retailerList.size();
		}
		count = mongoTemplate.count(new Query(), retailerService.getMyCollection(Retailer.class));
		logger.info("Count : " + count);
		logger.info("Records retrieved : " + countOfRetailerListReturned);
		//retailerList = retailerPageList.getContent();
		//retailerDTOList = RetailerMapper.map(retailerPageList);
		
		jqGridResponse.setPage(1);
		
		if(retailerList!= null) {
			
			jqGridResponse.setRecords(countOfRetailerListReturned);
			jqGridResponse.setTotal((int) count);
			jqGridResponse.setPage(page);
		}
		jqGridResponse.setRows(retailerList);
		return jqGridResponse;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MessageValue  save(Retailer retailer) {
		MessageValue message = new MessageValue();
		retailer.setUserPID(getCurrentUserPID());
		if(retailer.getId()!=null && retailer.getId().equals("_empty")) {
			retailer.setId(null);
		}
		retailerService.save(retailer);

		message.setMessage("Retailer created");
		// message.setDetailedMessage("http://farm4.staticflickr.com/3510/3208351775_e06db18ee4.jpg");

		return message;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	MessageValue delete(@RequestParam("oper") String oper,
			@RequestParam("id") String pid) {
		MessageValue message = new MessageValue();
		retailerService.delete(pid);
		message.setMessage("Retailer deleted");
		return message;
	}
	@RequestMapping(value="/searchRetailers", method=RequestMethod.GET)
	@ResponseBody
	public List<Retailer> searchRetailers(@RequestParam(value="term", required=false) String searchTerm) {
		List<Retailer> retailerCategories = retailerService.searchMyRetailers(searchTerm);
		return retailerCategories;
	}
}
